/**
 *    Copyright (C) 2008  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_TOOLS_REFLECTION_HPP
#define	_CUBEFS_CORE_TOOLS_REFLECTION_HPP

#include <cubefs/api/metadata.hpp>
#include <cubefs/core/api/api.hpp>

namespace cubefs    {
namespace tools     {

using namespace cubefs::api;

inline BigInt PtrToLong(const void *ptr) {
    return (BigInt)ptr;
}

template <typename T> struct FieldFactory;

template <typename Type>
struct FieldFactory {
    static void create(MetadataList &list, const Type &field, const string &name, Long &abi_ptr) {
        list.push_back(new MetadataGroupImpl(name, field.GetFields(abi_ptr)));
    }
};

template <typename Type>
class BitField{};

template <typename Type>
struct FieldFactory<BitField<Type> > {
    static void create(MetadataList &list, const Type &field, const string &name, Long offset, Long &abi_ptr) {
        list.push_back(new FlagFieldImpl(PtrToLong(&field), abi_ptr, name, offset));
    }

    static void create(MetadataList &list, const Type &field, const string &name, Long offset, Long count, Long &abi_ptr) {
        list.push_back(new FlagFieldImpl(PtrToLong(&field), abi_ptr, name, offset, count));
    }
};


#define CUBEFS_TYPED_FIELD(Type)                                                \
template <> struct FieldFactory<Type> {                                         \
    static void create(MetadataList &list, const Type &field, const string &name, Long &abi_ptr) {\
        list.push_back(new TypedFieldImpl<Type>(PtrToLong(&field), abi_ptr, name)); \
        abi_ptr += sizeof(Type);                                                \
    }                                                                           \
                                                                                \
    static void create(MetadataList &list, const Type &field, const string &name, Long size, Long &abi_ptr) {\
        list.push_back(new TypedFieldImpl<Type>(PtrToLong(&field), abi_ptr, name, size)); \
        abi_ptr += size * sizeof(Type);                                         \
    }                                                                           \
}


CUBEFS_TYPED_FIELD(Byte);
CUBEFS_TYPED_FIELD(Short);
CUBEFS_TYPED_FIELD(Int);
CUBEFS_TYPED_FIELD(BigInt);
CUBEFS_TYPED_FIELD(UByte);
CUBEFS_TYPED_FIELD(UShort);
CUBEFS_TYPED_FIELD(UInt);


#undef CUBEFS_TYPED_FIELD


}
}



#endif	/* _CUBEFS_CORE_REFLECTION_HPP */
